﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Testify.Tools
{
    public partial class KJSTools : System.Web.UI.Page
    {
        public System.Data.DataTable objDTtable;
        public System.Data.DataRow objDRtable;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                makeTable();
                addRow();
            }
        }



        protected void updateRows(object sender, EventArgs e)
        {
            addRow();
        }

        protected void addRow()
        {
            this.lblErrorMsg.Text = String.Empty;

            objDTtable = (System.Data.DataTable)Session["Table"];
            objDTtable.Clear();

            foreach (RepeaterItem r in this.rptQuestion.Items)
            {
                if (!String.IsNullOrEmpty((r.FindControl("txtQuestion") as TextBox).Text.Trim()) && !String.IsNullOrEmpty((r.FindControl("txtAnswer") as TextBox).Text.Trim()))
                {
                    objDRtable = objDTtable.NewRow();
                    objDRtable["Question"] = (r.FindControl("txtQuestion") as TextBox).Text;
                    objDRtable["Answer"] = (r.FindControl("txtAnswer") as TextBox).Text;
                    objDTtable.Rows.Add(objDRtable);
                }
            }

            objDRtable = objDTtable.NewRow();
            objDTtable.Rows.Add(objDRtable);

            Session["Table"] = objDTtable;

            this.rptQuestion.DataSource = objDTtable;
            this.rptQuestion.DataBind();
        }

        protected void makeTable()
        {
            // Create table for shipment 1
            objDTtable = new System.Data.DataTable("TrackTable");

            objDTtable.Columns.Add("Question", typeof(String));
            objDTtable.Columns.Add("Answer", typeof(String));
            Session["Table"] = objDTtable;
        }

        protected void rptQuestion_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex >= 0)
            {
                if (String.IsNullOrEmpty((e.Item.FindControl("txtQuestion") as TextBox).Text.Trim()))
                {
                    (e.Item.FindControl("txtQuestion") as TextBox).Focus();
                }
            }
        }

        protected void Tick(object sender, EventArgs e)
        {
            this.lblTimer.Text = (int.Parse(this.lblTimer.Text) - 1).ToString();
        }
    }
}